<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>JavaScript常用的代码片段 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,Snippet,">
  

  <meta name="description" content="转义特殊字符为html实体  HtmlEncode: function(str)&amp;#123;	return str.replace(/&amp;amp;/g, &apos;&amp;amp;&apos;).replace(/\&quot;/g, &apos;&quot;&apos;).replace(/&amp;lt;/g, &apos;&amp;lt;&apos;).replace(/&amp;gt;/g, &apos;&amp;gt;&apos;).replace(/&apos;/g, &apos;&apos;&apos;);&amp;#125;,  验证是否为有效的手机电话号码">
<meta name="keywords" content="JavaScript,Snippet">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript常用的代码片段">
<meta property="og:url" content="http://blog.poetries.top/2016/10/27/JavaScript-code-snippets/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="转义特殊字符为html实体  HtmlEncode: function(str)&amp;#123;	return str.replace(/&amp;amp;/g, &apos;&amp;amp;&apos;).replace(/\&quot;/g, &apos;&quot;&apos;).replace(/&amp;lt;/g, &apos;&amp;lt;&apos;).replace(/&amp;gt;/g, &apos;&amp;gt;&apos;).replace(/&apos;/g, &apos;&apos;&apos;);&amp;#125;,  验证是否为有效的手机电话号码">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.898Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript常用的代码片段">
<meta name="twitter:description" content="转义特殊字符为html实体  HtmlEncode: function(str)&amp;#123;	return str.replace(/&amp;amp;/g, &apos;&amp;amp;&apos;).replace(/\&quot;/g, &apos;&quot;&apos;).replace(/&amp;lt;/g, &apos;&amp;lt;&apos;).replace(/&amp;gt;/g, &apos;&amp;gt;&apos;).replace(/&apos;/g, &apos;&apos;&apos;);&amp;#125;,  验证是否为有效的手机电话号码">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>





<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-JavaScript-code-snippets" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">JavaScript常用的代码片段</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2016.10.27</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 4.4k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 24分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <ul>
<li>转义特殊字符为html实体</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">HtmlEncode: <span class="function"><span class="keyword">function</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> str.replace(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>).replace(<span class="regexp">/\"/g</span>, <span class="string">'"'</span>).replace(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>).replace(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>).replace(<span class="regexp">/'/g</span>, <span class="string">''</span><span class="string">');</span></span><br><span class="line"><span class="string">&#125;,</span></span><br></pre></td></tr></table></figure>
<ul>
<li>验证是否为有效的手机电话号码</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">IsMobile: <span class="function"><span class="keyword">function</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> rp = <span class="regexp">/^1[3|4|5|7|8][0-9]\d&#123;4,8&#125;$/</span>;</span><br><span class="line">	<span class="keyword">return</span> rp.test(str);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li>验证是否为有效的座机电话号码</li>
</ul>
<a id="more"></a>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">IsTel: <span class="function"><span class="keyword">function</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> rp = <span class="regexp">/^([0-9]&#123;3,4&#125;-)?[0-9]&#123;7,8&#125;$/</span>;</span><br><span class="line">	<span class="keyword">return</span> rp.test(str);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是那种类型的浏览器</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">WhichBrowser: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> userAgent = navigator.userAgent;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> isOpera = userAgent.indexOf(<span class="string">"Opera"</span>) &gt; <span class="number">-1</span>;</span><br><span class="line">	<span class="keyword">var</span> isIE = userAgent.indexOf(<span class="string">"compatible"</span>) &gt; <span class="number">-1</span> &amp;&amp; userAgent.indexOf(<span class="string">"MSIE"</span>) &gt; <span class="number">-1</span> &amp;&amp; !isOpera;</span><br><span class="line">	<span class="keyword">var</span> isFF = userAgent.indexOf(<span class="string">"Firefox"</span>) &gt; <span class="number">-1</span>;</span><br><span class="line">	<span class="keyword">var</span> isCH = userAgent.indexOf(<span class="string">"Chrome"</span>) &gt; <span class="number">-1</span>;</span><br><span class="line">	<span class="keyword">var</span> isSafari = userAgent.indexOf(<span class="string">"Safari"</span>) &gt; <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (isIE)&#123;</span><br><span class="line">		<span class="keyword">var</span> IE5 = IE55 = IE6 = IE7 = IE8 = <span class="literal">false</span>;</span><br><span class="line">		<span class="keyword">var</span> reIE = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"MSIE (\\d+\\.\\d+);"</span>);</span><br><span class="line">		reIE.test(userAgent);</span><br><span class="line">		<span class="keyword">var</span> fIEVersion = <span class="built_in">parseFloat</span>(<span class="built_in">RegExp</span>[<span class="string">"$1"</span>]);</span><br><span class="line">		IE55 = fIEVersion == <span class="number">5.5</span>;</span><br><span class="line">		IE6 = fIEVersion == <span class="number">6.0</span>;</span><br><span class="line">		IE7 = fIEVersion == <span class="number">7.0</span>;</span><br><span class="line">		IE8 = fIEVersion == <span class="number">8.0</span>;</span><br><span class="line">		<span class="keyword">if</span> (IE55) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"IE55"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (IE6) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"IE6"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (IE7) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"IE7"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (IE8) &#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="string">"IE8"</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (isFF) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Firefox"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (isCH) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Chrome"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (isOpera) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Opera"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (isSafari) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Safari"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li>获取客户端浏览器cookie</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">GetCookie: <span class="function"><span class="keyword">function</span>(<span class="params">c_name</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(<span class="built_in">document</span>.cookie.length&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		c_start = <span class="built_in">document</span>.cookie.indexOf(c_name + <span class="string">'='</span>);</span><br><span class="line">		<span class="keyword">if</span>(c_start != <span class="number">-1</span>)&#123;</span><br><span class="line">			c_start = c_start + c_name.length + <span class="number">1</span>;</span><br><span class="line">			c_end = <span class="built_in">document</span>.cookie.indexOf(<span class="string">';'</span>,c_start);</span><br><span class="line">			<span class="keyword">if</span> (c_end==<span class="number">-1</span>) c_end = <span class="built_in">document</span>.cookie.length;</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">unescape</span>(<span class="built_in">document</span>.cookie.substring(c_start,c_end));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="string">''</span>;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li>设置客户端浏览器cookie</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">SetCookie: <span class="function"><span class="keyword">function</span>(<span class="params">c_name, value, expiredays</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> exdate = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">	exdate.setDate(exdate.getDate() + expiredays);</span><br><span class="line">	<span class="built_in">document</span>.cookie = c_name + <span class="string">"="</span> + <span class="built_in">escape</span>(value) + ((expiredays==<span class="literal">null</span>) ? <span class="string">""</span> : <span class="string">";expires="</span>+exdate.toGMTString());</span><br><span class="line">&#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>字符串长度截取</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">cutstr</span>(<span class="params">str, len</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> temp,</span><br><span class="line">        icount = <span class="number">0</span>,</span><br><span class="line">        patrn = <span class="regexp">/[^\x00-\xff]/</span>，</span><br><span class="line">        strre = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (icount &lt; len - <span class="number">1</span>) &#123;</span><br><span class="line">            temp = str.substr(i, <span class="number">1</span>);</span><br><span class="line">                <span class="keyword">if</span> (patrn.exec(temp) == <span class="literal">null</span>) &#123;</span><br><span class="line">                   icount = icount + <span class="number">1</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                icount = icount + <span class="number">2</span></span><br><span class="line">            &#125;</span><br><span class="line">            strre += temp</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> strre + <span class="string">"..."</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>替换全部</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.replaceAll = <span class="function"><span class="keyword">function</span>(<span class="params">s1, s2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="keyword">new</span> <span class="built_in">RegExp</span>(s1, <span class="string">"gm"</span>), s2)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>清除空格</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.trim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> reExtraSpace = <span class="regexp">/^\s*(.*?)\s+$/</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.replace(reExtraSpace, <span class="string">"$1"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>清除左空格/右空格</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ltrim</span>(<span class="params">s</span>)</span>&#123; <span class="keyword">return</span> s.replace( <span class="regexp">/^(\s*|　*)/</span>, <span class="string">""</span>); &#125; </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">rtrim</span>(<span class="params">s</span>)</span>&#123; <span class="keyword">return</span> s.replace( <span class="regexp">/(\s*|　*)$/</span>, <span class="string">""</span>); &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否以某个字符串开头</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.startWith = <span class="function"><span class="keyword">function</span> (<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.indexOf(s) == <span class="number">0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否以某个字符串结束</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.endWith = <span class="function"><span class="keyword">function</span> (<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> d = <span class="keyword">this</span>.length - s.length;</span><br><span class="line">    <span class="keyword">return</span> (d &gt;= <span class="number">0</span> &amp;&amp; <span class="keyword">this</span>.lastIndexOf(s) == d)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>转义html标签</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">HtmlEncode</span>(<span class="params">text</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> text.replace(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>).replace(<span class="regexp">/\"/g</span>, <span class="string">'"'</span>).replace(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>).replace(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>时间日期格式转换</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">Date</span>.prototype.Format = <span class="function"><span class="keyword">function</span>(<span class="params">formatStr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> str = formatStr;</span><br><span class="line">    <span class="keyword">var</span> Week = [<span class="string">'日'</span>, <span class="string">'一'</span>, <span class="string">'二'</span>, <span class="string">'三'</span>, <span class="string">'四'</span>, <span class="string">'五'</span>, <span class="string">'六'</span>];</span><br><span class="line">    str = str.replace(<span class="regexp">/yyyy|YYYY/</span>, <span class="keyword">this</span>.getFullYear());</span><br><span class="line">    str = str.replace(<span class="regexp">/yy|YY/</span>, (<span class="keyword">this</span>.getYear() % <span class="number">100</span>) &gt; <span class="number">9</span> ? (<span class="keyword">this</span>.getYear() % <span class="number">100</span>).toString() : <span class="string">'0'</span> + (<span class="keyword">this</span>.getYear() % <span class="number">100</span>));</span><br><span class="line">    str = str.replace(<span class="regexp">/MM/</span>, (<span class="keyword">this</span>.getMonth() + <span class="number">1</span>) &gt; <span class="number">9</span> ? (<span class="keyword">this</span>.getMonth() + <span class="number">1</span>).toString() : <span class="string">'0'</span> + (<span class="keyword">this</span>.getMonth() + <span class="number">1</span>));</span><br><span class="line">    str = str.replace(<span class="regexp">/M/g</span>, (<span class="keyword">this</span>.getMonth() + <span class="number">1</span>));</span><br><span class="line">    str = str.replace(<span class="regexp">/w|W/g</span>, Week[<span class="keyword">this</span>.getDay()]);</span><br><span class="line">    str = str.replace(<span class="regexp">/dd|DD/</span>, <span class="keyword">this</span>.getDate() &gt; <span class="number">9</span> ? <span class="keyword">this</span>.getDate().toString() : <span class="string">'0'</span> + <span class="keyword">this</span>.getDate());</span><br><span class="line">    str = str.replace(<span class="regexp">/d|D/g</span>, <span class="keyword">this</span>.getDate());</span><br><span class="line">    str = str.replace(<span class="regexp">/hh|HH/</span>, <span class="keyword">this</span>.getHours() &gt; <span class="number">9</span> ? <span class="keyword">this</span>.getHours().toString() : <span class="string">'0'</span> + <span class="keyword">this</span>.getHours());</span><br><span class="line">    str = str.replace(<span class="regexp">/h|H/g</span>, <span class="keyword">this</span>.getHours());</span><br><span class="line">    str = str.replace(<span class="regexp">/mm/</span>, <span class="keyword">this</span>.getMinutes() &gt; <span class="number">9</span> ? <span class="keyword">this</span>.getMinutes().toString() : <span class="string">'0'</span> + <span class="keyword">this</span>.getMinutes());</span><br><span class="line">    str = str.replace(<span class="regexp">/m/g</span>, <span class="keyword">this</span>.getMinutes());</span><br><span class="line">    str = str.replace(<span class="regexp">/ss|SS/</span>, <span class="keyword">this</span>.getSeconds() &gt; <span class="number">9</span> ? <span class="keyword">this</span>.getSeconds().toString() : <span class="string">'0'</span> + <span class="keyword">this</span>.getSeconds());</span><br><span class="line">    str = str.replace(<span class="regexp">/s|S/g</span>, <span class="keyword">this</span>.getSeconds());</span><br><span class="line">    <span class="keyword">return</span> str</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否为数字类型</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isDigit</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> patrn = <span class="regexp">/^[0-9]*$/</span>;</span><br><span class="line">    <span class="keyword">if</span> (patrn.exec(value) == <span class="literal">null</span> || value == <span class="string">""</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>设置cookie值</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setCookie</span>(<span class="params">name, value, Hours</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> d = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    <span class="keyword">var</span> offset = <span class="number">8</span>;</span><br><span class="line">    <span class="keyword">var</span> utc = d.getTime() + (d.getTimezoneOffset() * <span class="number">60000</span>);</span><br><span class="line">    <span class="keyword">var</span> nd = utc + (<span class="number">3600000</span> * offset);</span><br><span class="line">    <span class="keyword">var</span> exp = <span class="keyword">new</span> <span class="built_in">Date</span>(nd);</span><br><span class="line">    exp.setTime(exp.getTime() + Hours * <span class="number">60</span> * <span class="number">60</span> * <span class="number">1000</span>);</span><br><span class="line">    <span class="built_in">document</span>.cookie = name + <span class="string">"="</span> + <span class="built_in">escape</span>(value) + <span class="string">";path=/;expires="</span> + exp.toGMTString() + <span class="string">";domain=360doc.com;"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取cookie值</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getCookie</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> arr = <span class="built_in">document</span>.cookie.match(<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"(^| )"</span> + name + <span class="string">"=([^;]*)(;|$)"</span>));</span><br><span class="line">    <span class="keyword">if</span> (arr != <span class="literal">null</span>) <span class="keyword">return</span> <span class="built_in">unescape</span>(arr[<span class="number">2</span>]);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>加入收藏夹</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">AddFavorite</span>(<span class="params">sURL, sTitle</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="built_in">window</span>.external.addFavorite(sURL, sTitle)</span><br><span class="line">    &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="built_in">window</span>.sidebar.addPanel(sTitle, sURL, <span class="string">""</span>)</span><br><span class="line">        &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">            alert(<span class="string">"加入收藏失败，请使用Ctrl+D进行添加"</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>设为首页</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setHomepage</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">document</span>.all) &#123;</span><br><span class="line">        <span class="built_in">document</span>.body.style.behavior = <span class="string">'url(#default#homepage)'</span>;</span><br><span class="line">        <span class="built_in">document</span>.body.setHomePage(<span class="string">'http://w3cboy.com'</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">window</span>.sidebar) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">window</span>.netscape) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                netscape.security.PrivilegeManager.enablePrivilege(<span class="string">"UniversalXPConnect"</span>)</span><br><span class="line">            &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">                alert(<span class="string">"该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true"</span>)</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">var</span> prefs = Components.classes[<span class="string">'@mozilla.org/preferences-service;1'</span>].getService(Components.interfaces.nsIPrefBranch);</span><br><span class="line">        prefs.setCharPref(<span class="string">'browser.startup.homepage'</span>, <span class="string">'http://w3cboy.com'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>加载样式文件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">LoadStyle</span>(<span class="params">url</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="built_in">document</span>.createStyleSheet(url)</span><br><span class="line">    &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">        <span class="keyword">var</span> cssLink = <span class="built_in">document</span>.createElement(<span class="string">'link'</span>);</span><br><span class="line">        cssLink.rel = <span class="string">'stylesheet'</span>;</span><br><span class="line">        cssLink.type = <span class="string">'text/css'</span>;</span><br><span class="line">        cssLink.href = url;</span><br><span class="line">        <span class="keyword">var</span> head = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>];</span><br><span class="line">        head.appendChild(cssLink)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>返回脚本内容</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">evalscript</span>(<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(s.indexOf(<span class="string">'&lt;script'</span>) == <span class="number">-1</span>) <span class="keyword">return</span> s;</span><br><span class="line">    <span class="keyword">var</span> p = <span class="regexp">/&lt;script[^\&gt;]*?&gt;([^\x00]*?)&lt;\/script&gt;/ig</span>;</span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">while</span>(arr = p.exec(s)) &#123;</span><br><span class="line">        <span class="keyword">var</span> p1 = <span class="regexp">/&lt;script[^\&gt;]*?src=\"([^\&gt;]*?)\"[^\&gt;]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?&gt;&lt;\/script&gt;/i</span>;</span><br><span class="line">        <span class="keyword">var</span> arr1 = [];</span><br><span class="line">        arr1 = p1.exec(arr[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">if</span>(arr1) &#123;</span><br><span class="line">            appendscript(arr1[<span class="number">1</span>], <span class="string">''</span>, arr1[<span class="number">2</span>], arr1[<span class="number">3</span>]);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            p1 = <span class="regexp">/&lt;script(.*?)&gt;([^\x00]+?)&lt;\/script&gt;/i</span>;</span><br><span class="line">            arr1 = p1.exec(arr[<span class="number">0</span>]);</span><br><span class="line">            appendscript(<span class="string">''</span>, arr1[<span class="number">2</span>], arr1[<span class="number">1</span>].indexOf(<span class="string">'reload='</span>) != <span class="number">-1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>清除脚本内容</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">stripscript</span>(<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> s.replace(<span class="regexp">/&lt;script.*?&gt;.*?&lt;\/script&gt;/ig</span>, <span class="string">''</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>动态加载脚本文件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">appendscript</span>(<span class="params">src, text, reload, charset</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> id = hash(src + text);</span><br><span class="line">    <span class="keyword">if</span>(!reload &amp;&amp; in_array(id, evalscripts)) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">if</span>(reload &amp;&amp; $(id)) &#123;</span><br><span class="line">        $(id).parentNode.removeChild($(id));</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    evalscripts.push(id);</span><br><span class="line">    <span class="keyword">var</span> scriptNode = <span class="built_in">document</span>.createElement(<span class="string">"script"</span>);</span><br><span class="line">    scriptNode.type = <span class="string">"text/javascript"</span>;</span><br><span class="line">    scriptNode.id = id;</span><br><span class="line">    scriptNode.charset = charset ? charset : (BROWSER.firefox ? <span class="built_in">document</span>.characterSet : <span class="built_in">document</span>.charset);</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(src) &#123;</span><br><span class="line">            scriptNode.src = src;</span><br><span class="line">            scriptNode.onloadDone = <span class="literal">false</span>;</span><br><span class="line">            scriptNode.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">                scriptNode.onloadDone = <span class="literal">true</span>;</span><br><span class="line">                JSLOADED[src] = <span class="number">1</span>;</span><br><span class="line">             &#125;;</span><br><span class="line">             scriptNode.onreadystatechange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">                 <span class="keyword">if</span>((scriptNode.readyState == <span class="string">'loaded'</span> || scriptNode.readyState == <span class="string">'complete'</span>) &amp;&amp; !scriptNode.onloadDone) &#123;</span><br><span class="line">                    scriptNode.onloadDone = <span class="literal">true</span>;</span><br><span class="line">                    JSLOADED[src] = <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">             &#125;;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(text)&#123;</span><br><span class="line">            scriptNode.text = text;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].appendChild(scriptNode);</span><br><span class="line">    &#125; <span class="keyword">catch</span>(e) &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>返回按ID检索的元素对象</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">$</span>(<span class="params">id</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !id ? <span class="literal">null</span> : <span class="built_in">document</span>.getElementById(id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>跨浏览器绑定事件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addEventSamp</span>(<span class="params">obj,evt,fn</span>)</span>&#123; </span><br><span class="line">    <span class="keyword">if</span>(!oTarget)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="keyword">if</span> (obj.addEventListener) &#123; </span><br><span class="line">        obj.addEventListener(evt, fn, <span class="literal">false</span>); </span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(obj.attachEvent)&#123; </span><br><span class="line">        obj.attachEvent(<span class="string">'on'</span>+evt,fn); </span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        oTarget[<span class="string">"on"</span> + sEvtType] = fn;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>跨浏览器删除事件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">delEvt</span>(<span class="params">obj,evt,fn</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(!obj)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="keyword">if</span>(obj.addEventListener)&#123;</span><br><span class="line">        obj.addEventListener(evt,fn,<span class="literal">false</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(oTarget.attachEvent)&#123;</span><br><span class="line">        obj.attachEvent(<span class="string">"on"</span> + evt,fn);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        obj[<span class="string">"on"</span> + evt] = fn;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>为元素添加on方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Element.prototype.on = Element.prototype.addEventListener;</span><br><span class="line"> </span><br><span class="line">NodeList.prototype.on = <span class="function"><span class="keyword">function</span> (<span class="params">event, fn</span>) </span>&#123;、</span><br><span class="line">    [][<span class="string">'forEach'</span>].call(<span class="keyword">this</span>, <span class="function"><span class="keyword">function</span> (<span class="params">el</span>) </span>&#123;</span><br><span class="line">        el.on(event, fn);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>为元素添加trigger方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Element.prototype.trigger = <span class="function"><span class="keyword">function</span> (<span class="params">type, data</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> event = <span class="built_in">document</span>.createEvent(<span class="string">'HTMLEvents'</span>);</span><br><span class="line">    event.initEvent(type, <span class="literal">true</span>, <span class="literal">true</span>);</span><br><span class="line">    event.data = data || &#123;&#125;;</span><br><span class="line">    event.eventName = type;</span><br><span class="line">    event.target = <span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">this</span>.dispatchEvent(event);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line">NodeList.prototype.trigger = <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line">    [][<span class="string">'forEach'</span>].call(<span class="keyword">this</span>, <span class="function"><span class="keyword">function</span> (<span class="params">el</span>) </span>&#123;</span><br><span class="line">        el[<span class="string">'trigger'</span>](event);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>检验URL链接是否有效</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUrlState</span>(<span class="params">URL</span>)</span>&#123; </span><br><span class="line">    <span class="keyword">var</span> xmlhttp = <span class="keyword">new</span> ActiveXObject(<span class="string">"microsoft.xmlhttp"</span>); </span><br><span class="line">    xmlhttp.Open(<span class="string">"GET"</span>,URL, <span class="literal">false</span>);  </span><br><span class="line">    <span class="keyword">try</span>&#123;  </span><br><span class="line">            xmlhttp.Send(); </span><br><span class="line">    &#125;<span class="keyword">catch</span>(e)&#123;</span><br><span class="line">    &#125;<span class="keyword">finally</span>&#123; </span><br><span class="line">        <span class="keyword">var</span> result = xmlhttp.responseText; </span><br><span class="line">        <span class="keyword">if</span>(result)&#123;</span><br><span class="line">            <span class="keyword">if</span>(xmlhttp.Status==<span class="number">200</span>)&#123; </span><br><span class="line">                <span class="keyword">return</span>(<span class="literal">true</span>); </span><br><span class="line">             &#125;<span class="keyword">else</span>&#123; </span><br><span class="line">                   <span class="keyword">return</span>(<span class="literal">false</span>); </span><br><span class="line">             &#125; </span><br><span class="line">         &#125;<span class="keyword">else</span>&#123; </span><br><span class="line">             <span class="keyword">return</span>(<span class="literal">false</span>); </span><br><span class="line">         &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>格式化CSS样式代码</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">formatCss</span>(<span class="params">s</span>)</span>&#123;<span class="comment">//格式化代码</span></span><br><span class="line">    s = s.replace(<span class="regexp">/\s*([\&#123;\&#125;\:\;\,])\s*/g</span>, <span class="string">"$1"</span>);</span><br><span class="line">    s = s.replace(<span class="regexp">/;\s*;/g</span>, <span class="string">";"</span>); <span class="comment">//清除连续分号</span></span><br><span class="line">    s = s.replace(<span class="regexp">/\,[\s\.\#\d]*&#123;/g</span>, <span class="string">"&#123;"</span>);</span><br><span class="line">    s = s.replace(<span class="regexp">/([^\s])\&#123;([^\s])/g</span>, <span class="string">"$1 &#123;\n\t$2"</span>);</span><br><span class="line">    s = s.replace(<span class="regexp">/([^\s])\&#125;([^\n]*)/g</span>, <span class="string">"$1\n&#125;\n$2"</span>);</span><br><span class="line">    s = s.replace(<span class="regexp">/([^\s]);([^\s\&#125;])/g</span>, <span class="string">"$1;\n\t$2"</span>);</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>压缩CSS样式代码</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">compressCss</span> (<span class="params">s</span>) </span>&#123;<span class="comment">//压缩代码</span></span><br><span class="line">    s = s.replace(<span class="regexp">/\/\*(.|\n)*?\*\//g</span>, <span class="string">""</span>); <span class="comment">//删除注释</span></span><br><span class="line">    s = s.replace(<span class="regexp">/\s*([\&#123;\&#125;\:\;\,])\s*/g</span>, <span class="string">"$1"</span>);</span><br><span class="line">    s = s.replace(<span class="regexp">/\,[\s\.\#\d]*\&#123;/g</span>, <span class="string">"&#123;"</span>); <span class="comment">//容错处理</span></span><br><span class="line">    s = s.replace(<span class="regexp">/;\s*;/g</span>, <span class="string">";"</span>); <span class="comment">//清除连续分号</span></span><br><span class="line">    s = s.match(<span class="regexp">/^\s*(\S+(\s+\S+)*)\s*$/</span>); <span class="comment">//去掉首尾空白</span></span><br><span class="line">    <span class="keyword">return</span> (s == <span class="literal">null</span>) ? <span class="string">""</span> : s[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取当前路径</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> currentPageUrl = <span class="string">""</span>;</span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="keyword">this</span>.href === <span class="string">"undefined"</span>) &#123;</span><br><span class="line">    currentPageUrl = <span class="built_in">document</span>.location.toString().toLowerCase();</span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    currentPageUrl = <span class="keyword">this</span>.href.toString().toLowerCase();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否移动设备</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isMobile</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="keyword">this</span>._isMobile === <span class="string">'boolean'</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>._isMobile;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> screenWidth = <span class="keyword">this</span>.getScreenWidth();</span><br><span class="line">    <span class="keyword">var</span> fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport ||rendererModel.runningExperiments.fixviewport;</span><br><span class="line">    <span class="keyword">var</span> fixViewPortsExperimentRunning = fixViewPortsExperiment &amp;&amp; (fixViewPortsExperiment.toLowerCase() === <span class="string">"new"</span>);</span><br><span class="line">    <span class="keyword">if</span>(!fixViewPortsExperiment)&#123;</span><br><span class="line">        <span class="keyword">if</span>(!<span class="keyword">this</span>.isAppleMobileDevice())&#123;</span><br><span class="line">            screenWidth = screenWidth/<span class="built_in">window</span>.devicePixelRatio;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> isMobileScreenSize = screenWidth &lt; <span class="number">600</span>;</span><br><span class="line">    <span class="keyword">var</span> isMobileUserAgent = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">this</span>._isMobile = isMobileScreenSize &amp;&amp; <span class="keyword">this</span>.isTouchScreen();</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>._isMobile;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否移动设备访问</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isMobileUserAgent</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="regexp">/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i</span>.test(<span class="built_in">window</span>.navigator.userAgent.toLowerCase()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否苹果移动设备访问</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isAppleMobileDevice</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="regexp">/iphone|ipod|ipad|Macintosh/i</span>.test(navigator.userAgent.toLowerCase()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否安卓移动设备访问</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isAndroidMobileDevice</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="regexp">/android/i</span>.test(navigator.userAgent.toLowerCase()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否Touch屏幕</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isTouchScreen</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> ((<span class="string">'ontouchstart'</span> <span class="keyword">in</span> <span class="built_in">window</span>) || <span class="built_in">window</span>.DocumentTouch &amp;&amp; <span class="built_in">document</span> <span class="keyword">instanceof</span> DocumentTouch);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>判断是否打开视窗</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isViewportOpen</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !!<span class="built_in">document</span>.getElementById(<span class="string">'wixMobileViewport'</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取移动设备初始化大小</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getInitZoom</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(!<span class="keyword">this</span>._initZoom)&#123;</span><br><span class="line">        <span class="keyword">var</span> screenWidth = <span class="built_in">Math</span>.min(screen.height, screen.width);</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.isAndroidMobileDevice() &amp;&amp; !<span class="keyword">this</span>.isNewChromeOnAndroid())&#123;</span><br><span class="line">            screenWidth = screenWidth/<span class="built_in">window</span>.devicePixelRatio;</span><br><span class="line">        &#125;</span><br><span class="line">            <span class="keyword">this</span>._initZoom = screenWidth /<span class="built_in">document</span>.body.offsetWidth;</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>._initZoom;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取移动设备最大化大小</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getZoom</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> screenWidth = (<span class="built_in">Math</span>.abs(<span class="built_in">window</span>.orientation) === <span class="number">90</span>) ? <span class="built_in">Math</span>.max(screen.height, screen.width) : <span class="built_in">Math</span>.min(screen.height, screen.width);</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">this</span>.isAndroidMobileDevice() &amp;&amp; !<span class="keyword">this</span>.isNewChromeOnAndroid())&#123;</span><br><span class="line">        screenWidth = screenWidth/<span class="built_in">window</span>.devicePixelRatio;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> FixViewPortsExperiment = rendererModel.runningExperiments.FixViewport || rendererModel.runningExperiments.fixviewport;</span><br><span class="line">    <span class="keyword">var</span> FixViewPortsExperimentRunning = FixViewPortsExperiment &amp;&amp; (FixViewPortsExperiment === <span class="string">"New"</span> || FixViewPortsExperiment === <span class="string">"new"</span>);</span><br><span class="line">    <span class="keyword">if</span>(FixViewPortsExperimentRunning)&#123;</span><br><span class="line">        <span class="keyword">return</span> screenWidth / <span class="built_in">window</span>.innerWidth;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> screenWidth / <span class="built_in">document</span>.body.offsetWidth;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取移动设备屏幕宽度</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getScreenWidth</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> smallerSide = <span class="built_in">Math</span>.min(screen.width, screen.height);</span><br><span class="line">    <span class="keyword">var</span> fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport || rendererModel.runningExperiments.fixviewport;</span><br><span class="line">    <span class="keyword">var</span> fixViewPortsExperimentRunning = fixViewPortsExperiment &amp;&amp; (fixViewPortsExperiment.toLowerCase() === <span class="string">"new"</span>);</span><br><span class="line">    <span class="keyword">if</span>(fixViewPortsExperiment)&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.isAndroidMobileDevice() &amp;&amp; !<span class="keyword">this</span>.isNewChromeOnAndroid())&#123;</span><br><span class="line">            smallerSide = smallerSide/<span class="built_in">window</span>.devicePixelRatio;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> smallerSide;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>完美判断是否为网址</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">IsURL</span>(<span class="params">strUrl</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> regular = <span class="regexp">/^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&amp;?=+,.!\/~%\$]*)?)$/i</span></span><br><span class="line">    <span class="keyword">if</span> (regular.test(strUrl)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面高度</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageHeight</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> g = <span class="built_in">document</span>, a = g.body, f = g.documentElement, d = g.compatMode == <span class="string">"BackCompat"</span></span><br><span class="line">                    ? a</span><br><span class="line">                    : g.documentElement;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Math</span>.max(f.scrollHeight, a.scrollHeight, d.clientHeight);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面scrollLeft</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageScrollLeft</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="built_in">document</span>;</span><br><span class="line">    <span class="keyword">return</span> a.documentElement.scrollLeft || a.body.scrollLeft;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面可视宽度</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageViewWidth</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> d = <span class="built_in">document</span>, a = d.compatMode == <span class="string">"BackCompat"</span></span><br><span class="line">                    ? d.body</span><br><span class="line">                    : d.documentElement;</span><br><span class="line">    <span class="keyword">return</span> a.clientWidth;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面宽度</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageWidth</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> g = <span class="built_in">document</span>, a = g.body, f = g.documentElement, d = g.compatMode == <span class="string">"BackCompat"</span></span><br><span class="line">                    ? a</span><br><span class="line">                    : g.documentElement;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Math</span>.max(f.scrollWidth, a.scrollWidth, d.clientWidth);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面scrollTop</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageScrollTop</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="built_in">document</span>;</span><br><span class="line">    <span class="keyword">return</span> a.documentElement.scrollTop || a.body.scrollTop;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取页面可视高度</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPageViewHeight</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> d = <span class="built_in">document</span>, a = d.compatMode == <span class="string">"BackCompat"</span></span><br><span class="line">                    ? d.body</span><br><span class="line">                    : d.documentElement;</span><br><span class="line">    <span class="keyword">return</span> a.clientHeight;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>去掉url前缀</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">removeUrlPrefix</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">    a=a.replace(<span class="regexp">/：/g</span>,<span class="string">":"</span>).replace(<span class="regexp">/．/g</span>,<span class="string">"."</span>).replace(<span class="regexp">/／/g</span>,<span class="string">"/"</span>);</span><br><span class="line">    <span class="keyword">while</span>(trim(a).toLowerCase().indexOf(<span class="string">"http://"</span>)==<span class="number">0</span>)&#123;</span><br><span class="line">        a=trim(a.replace(<span class="regexp">/http:\/\//i</span>,<span class="string">""</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>随机数时间戳</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">uniqueId</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a=<span class="built_in">Math</span>.random,b=<span class="built_in">parseInt</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Number</span>(<span class="keyword">new</span> <span class="built_in">Date</span>()).toString()+b(<span class="number">10</span>*a())+b(<span class="number">10</span>*a())+b(<span class="number">10</span>*a());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>全角半角转换</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//iCase: 0全到半，1半到全，其他不转化</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">chgCase</span>(<span class="params">sStr,iCase</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> sStr != <span class="string">"string"</span> || sStr.length &lt;= <span class="number">0</span> || !(iCase === <span class="number">0</span> || iCase == <span class="number">1</span>))&#123;</span><br><span class="line">        <span class="keyword">return</span> sStr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> i,oRs=[],iCode;</span><br><span class="line">    <span class="keyword">if</span>(iCase)&#123;<span class="comment">/*半-&gt;全*/</span></span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;sStr.length;i+=<span class="number">1</span>)&#123; </span><br><span class="line">            iCode = sStr.charCodeAt(i);</span><br><span class="line">            <span class="keyword">if</span>(iCode == <span class="number">32</span>)&#123;</span><br><span class="line">                iCode = <span class="number">12288</span>;                                </span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(iCode &lt; <span class="number">127</span>)&#123;</span><br><span class="line">                iCode += <span class="number">65248</span>;</span><br><span class="line">            &#125;</span><br><span class="line">                oRs.push(<span class="built_in">String</span>.fromCharCode(iCode)); </span><br><span class="line">            &#125;                </span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;<span class="comment">/*全-&gt;半*/</span></span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;sStr.length;i+=<span class="number">1</span>)&#123; </span><br><span class="line">            iCode = sStr.charCodeAt(i);</span><br><span class="line">            <span class="keyword">if</span>(iCode == <span class="number">12288</span>)&#123;</span><br><span class="line">                iCode = <span class="number">32</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(iCode &gt; <span class="number">65280</span> &amp;&amp; iCode &lt; <span class="number">65375</span>)&#123;</span><br><span class="line">                iCode -= <span class="number">65248</span>;                                </span><br><span class="line">            &#125;</span><br><span class="line">                oRs.push(<span class="built_in">String</span>.fromCharCode(iCode)); </span><br><span class="line">         &#125;                </span><br><span class="line">    &#125;                </span><br><span class="line">    <span class="keyword">return</span> oRs.join(<span class="string">""</span>);                </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>确认是否键盘有效输入值</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkKey</span>(<span class="params">iKey</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(iKey == <span class="number">32</span> || iKey == <span class="number">229</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*空格和异常*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">47</span> &amp;&amp; iKey &lt; <span class="number">58</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*数字*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">64</span> &amp;&amp; iKey &lt; <span class="number">91</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*字母*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">95</span> &amp;&amp; iKey &lt; <span class="number">108</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*数字键盘1*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">108</span> &amp;&amp; iKey &lt; <span class="number">112</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*数字键盘2*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">185</span> &amp;&amp; iKey &lt; <span class="number">193</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*符号1*/</span></span><br><span class="line">    <span class="keyword">if</span>(iKey&gt;<span class="number">218</span> &amp;&amp; iKey &lt; <span class="number">223</span>)&#123;<span class="keyword">return</span> <span class="literal">true</span>;&#125;<span class="comment">/*符号2*/</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>日期格式化函数+调用方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">Date</span>.prototype.format = <span class="function"><span class="keyword">function</span>(<span class="params">format</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> o = &#123;</span><br><span class="line">        <span class="string">"M+"</span> : <span class="keyword">this</span>.getMonth()+<span class="number">1</span>, <span class="comment">//month</span></span><br><span class="line">        <span class="string">"d+"</span> : <span class="keyword">this</span>.getDate(),    <span class="comment">//day</span></span><br><span class="line">        <span class="string">"h+"</span> : <span class="keyword">this</span>.getHours(),   <span class="comment">//hour</span></span><br><span class="line">        <span class="string">"m+"</span> : <span class="keyword">this</span>.getMinutes(), <span class="comment">//minute</span></span><br><span class="line">        <span class="string">"s+"</span> : <span class="keyword">this</span>.getSeconds(), <span class="comment">//second</span></span><br><span class="line">        <span class="string">"q+"</span> : <span class="built_in">Math</span>.floor((<span class="keyword">this</span>.getMonth()+<span class="number">3</span>)/<span class="number">3</span>),  <span class="comment">//quarter</span></span><br><span class="line">        <span class="string">"S"</span> : <span class="keyword">this</span>.getMilliseconds() <span class="comment">//millisecond</span></span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">if</span>(<span class="regexp">/(y+)/</span>.test(format)) format=format.replace(<span class="built_in">RegExp</span>.$<span class="number">1</span>,</span><br><span class="line">(<span class="keyword">this</span>.getFullYear()+<span class="string">""</span>).substr(<span class="number">4</span> - <span class="built_in">RegExp</span>.$<span class="number">1.</span>length));</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> k <span class="keyword">in</span> o)&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"("</span>+ k +<span class="string">")"</span>).test(format))</span><br><span class="line">            format = format.replace(<span class="built_in">RegExp</span>.$<span class="number">1</span>,<span class="built_in">RegExp</span>.$<span class="number">1.</span>length==<span class="number">1</span> ? o[k] :(<span class="string">"00"</span>+ o[k]).substr((<span class="string">""</span>+ o[k]).length));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> format;</span><br><span class="line">&#125;</span><br><span class="line">alert(<span class="keyword">new</span> <span class="built_in">Date</span>().format(<span class="string">"yyyy-MM-dd hh:mm:ss"</span>));</span><br></pre></td></tr></table></figure>
<ul>
<li>常用的正则表达式</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">//正整数</span><br><span class="line">/^[0-9]*[1-9][0-9]*$/;</span><br><span class="line">//负整数</span><br><span class="line">/^-[0-9]*[1-9][0-9]*$/;</span><br><span class="line">//正浮点数</span><br><span class="line">/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;   </span><br><span class="line">//负浮点数</span><br><span class="line">/^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;  </span><br><span class="line">//浮点数</span><br><span class="line">/^(-?\d+)(\.\d+)?$/;</span><br><span class="line">//email地址</span><br><span class="line">/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;</span><br><span class="line">//url地址</span><br><span class="line">/^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$/;</span><br><span class="line">或：^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&amp;_~`@[\]\&apos;:+!]*([^&lt;&gt;\&quot;\&quot;])*$ </span><br><span class="line">//年/月/日（年-月-日、年.月.日）</span><br><span class="line">/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/;</span><br><span class="line">//匹配中文字符</span><br><span class="line">/[\u4e00-\u9fa5]/;</span><br><span class="line">//匹配帐号是否合法(字母开头，允许5-10字节，允许字母数字下划线)</span><br><span class="line">/^[a-zA-Z][a-zA-Z0-9_]&#123;4,9&#125;$/;</span><br><span class="line">//匹配空白行的正则表达式</span><br><span class="line">/\n\s*\r/;</span><br><span class="line">//匹配中国邮政编码</span><br><span class="line">/[1-9]\d&#123;5&#125;(?!\d)/;</span><br><span class="line">//匹配身份证</span><br><span class="line">/\d&#123;15&#125;|\d&#123;18&#125;/;</span><br><span class="line">//匹配国内电话号码</span><br><span class="line">/(\d&#123;3&#125;-|\d&#123;4&#125;-)?(\d&#123;8&#125;|\d&#123;7&#125;)?/;</span><br><span class="line">//匹配IP地址</span><br><span class="line">/((2[0-4]\d|25[0-5]|[01]?\d\d?)\.)&#123;3&#125;(2[0-4]\d|25[0-5]|[01]?\d\d?)/;</span><br><span class="line">//匹配首尾空白字符的正则表达式</span><br><span class="line">/^\s*|\s*$/;</span><br><span class="line">//匹配HTML标记的正则表达式</span><br><span class="line">&lt; (\S*?)[^&gt;]*&gt;.*?|&lt; .*? /&gt;;</span><br><span class="line">//sql 语句</span><br><span class="line">^(select|drop|delete|create|update|insert).*$</span><br><span class="line">//提取信息中的网络链接</span><br><span class="line">(h|H)(r|R)(e|E)(f|F) *= *(&apos;|&quot;)?(\w|\\|\/|\.)+(&apos;|&quot;| *|&gt;)? </span><br><span class="line">//提取信息中的邮件地址</span><br><span class="line">\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* </span><br><span class="line">//提取信息中的图片链接</span><br><span class="line">(s|S)(r|R)(c|C) *= *(&apos;|&quot;)?(\w|\\|\/|\.)+(&apos;|&quot;| *|&gt;)? </span><br><span class="line">//提取信息中的 IP 地址</span><br><span class="line">(\d+)\.(\d+)\.(\d+)\.(\d+)</span><br><span class="line">//取信息中的中国手机号码</span><br><span class="line">(86)*0*13\d&#123;9&#125; </span><br><span class="line">//提取信息中的中国邮政编码</span><br><span class="line">[1-9]&#123;1&#125;(\d+)&#123;5&#125; </span><br><span class="line">//提取信息中的浮点数（即小数）</span><br><span class="line">(-?\d*)\.?\d+ </span><br><span class="line">//提取信息中的任何数字</span><br><span class="line">(-?\d*)(\.\d+)?</span><br><span class="line">//电话区号</span><br><span class="line">^0\d&#123;2,3&#125;$</span><br><span class="line">//腾讯 QQ 号</span><br><span class="line">^[1-9]*[1-9][0-9]*$ </span><br><span class="line">//帐号（字母开头，允许 5-16 字节，允许字母数字下划线）</span><br><span class="line">^[a-zA-Z][a-zA-Z0-9_]&#123;4,15&#125;$ </span><br><span class="line">//中文、英文、数字及下划线</span><br><span class="line">^[\u4e00-\u9fa5_a-zA-Z0-9]+$</span><br></pre></td></tr></table></figure>
<ul>
<li>返回顶部的通用方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">backTop</span>(<span class="params">btnId</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> btn = <span class="built_in">document</span>.getElementById(btnId);</span><br><span class="line">    <span class="keyword">var</span> d = <span class="built_in">document</span>.documentElement;</span><br><span class="line">    <span class="keyword">var</span> b = <span class="built_in">document</span>.body;</span><br><span class="line">    <span class="built_in">window</span>.onscroll = <span class="keyword">set</span>;</span><br><span class="line">    btn.style.display = "none";</span><br><span class="line">    btn.onclick = function() &#123;</span><br><span class="line">        btn.style.display = <span class="string">"none"</span>;</span><br><span class="line">        <span class="built_in">window</span>.onscroll = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            d.scrollTop -= <span class="built_in">Math</span>.ceil((d.scrollTop + b.scrollTop) * <span class="number">0.1</span>);</span><br><span class="line">            b.scrollTop -= <span class="built_in">Math</span>.ceil((d.scrollTop + b.scrollTop) * <span class="number">0.1</span>);</span><br><span class="line">            <span class="keyword">if</span> ((d.scrollTop + b.scrollTop) == <span class="number">0</span>) clearInterval(btn.timer, <span class="built_in">window</span>.onscroll = <span class="keyword">set</span>);</span><br><span class="line">            &#125;, 10);</span><br><span class="line">    &#125;;</span><br><span class="line">    function <span class="keyword">set</span>() &#123;</span><br><span class="line">        btn.style.display = (d.scrollTop + b.scrollTop &gt; <span class="number">100</span>) ? <span class="string">'block'</span>: <span class="string">"none"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">backTop(<span class="string">'goTop'</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>获得URL中GET参数值</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 用法：如果地址是 test.htm?t1=1&amp;t2=2&amp;t3=3, 那么能取得：GET["t1"], GET["t2"], GET["t3"]</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_get</span>(<span class="params"></span>)</span>&#123; </span><br><span class="line">    querystr = <span class="built_in">window</span>.location.href.split(<span class="string">"?"</span>)</span><br><span class="line">    <span class="keyword">if</span>(querystr[<span class="number">1</span>])&#123;</span><br><span class="line">        GETs = querystr[<span class="number">1</span>].split(<span class="string">"&amp;"</span>);</span><br><span class="line">        GET = [];</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;GETs.length;i++)&#123;</span><br><span class="line">              tmp_arr = GETs.split(<span class="string">"="</span>)</span><br><span class="line">              key=tmp_arr[<span class="number">0</span>]</span><br><span class="line">              GET[key] = tmp_arr[<span class="number">1</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> querystr[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>打开一个窗体通用方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">openWindow</span>(<span class="params">url,windowName,width,height</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> x = <span class="built_in">parseInt</span>(screen.width / <span class="number">2.0</span>) - (width / <span class="number">2.0</span>); </span><br><span class="line">    <span class="keyword">var</span> y = <span class="built_in">parseInt</span>(screen.height / <span class="number">2.0</span>) - (height / <span class="number">2.0</span>);</span><br><span class="line">    <span class="keyword">var</span> isMSIE= (navigator.appName == <span class="string">"Microsoft Internet Explorer"</span>);</span><br><span class="line">    <span class="keyword">if</span> (isMSIE) &#123;</span><br><span class="line">        <span class="keyword">var</span> p = <span class="string">"resizable=1,location=no,scrollbars=no,width="</span>;</span><br><span class="line">        p = p+width;</span><br><span class="line">           p = p+<span class="string">",height="</span>;</span><br><span class="line">           p = p+height;</span><br><span class="line">        p = p+<span class="string">",left="</span>;</span><br><span class="line">        p = p+x;</span><br><span class="line">        p = p+<span class="string">",top="</span>;</span><br><span class="line">        p = p+y;</span><br><span class="line">        retval = <span class="built_in">window</span>.open(url, windowName, p);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> win = <span class="built_in">window</span>.open(url, <span class="string">"ZyiisPopup"</span>, <span class="string">"top="</span> + y + <span class="string">",left="</span> + x + <span class="string">",scrollbars="</span> + scrollbars + <span class="string">",dialog=yes,modal=yes,width="</span> + width + <span class="string">",height="</span> + height + <span class="string">",resizable=no"</span> );</span><br><span class="line">        <span class="built_in">eval</span>(<span class="string">"try &#123; win.resizeTo(width, height); &#125; catch(e) &#123; &#125;"</span>);</span><br><span class="line">        win.focus();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>提取页面代码中所有网址</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> aa = <span class="built_in">document</span>.documentElement.outerHTML.match(<span class="regexp">/(url\(|src=|href=)[\"\']*([^\"\'\(\)\&lt;\&gt;\[\] ]+)[\"\'\)]*|(http:\/\/[\w\-\.]+[^\"\'\(\)\&lt;\&gt;\[\] ]+)/ig</span>).join(<span class="string">"\r\n"</span>).replace(<span class="regexp">/^(src=|href=|url\()[\"\']*|[\"\'\&gt;\) ]*$/igm</span>,<span class="string">""</span>);</span><br><span class="line">alert(aa);</span><br></pre></td></tr></table></figure>
<ul>
<li>清除相同的数组</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.unique=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> x=<span class="keyword">this</span>.split(<span class="regexp">/[\r\n]+/</span>);</span><br><span class="line">    <span class="keyword">var</span> y=<span class="string">''</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;x.length;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(!<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"^"</span>+x.replace(<span class="regexp">/([^\w])/ig</span>,<span class="string">"\\$1"</span>)+<span class="string">"$"</span>,<span class="string">"igm"</span>).test(y))&#123;</span><br><span class="line">            y+=x+<span class="string">"\r\n"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> y</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>按字母排序，对每行进行数组排序</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">SetSort</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> text=K1.value.split(<span class="regexp">/[\r\n]/</span>).sort().join(<span class="string">"\r\n"</span>);<span class="comment">//顺序</span></span><br><span class="line">    <span class="keyword">var</span> test=K1.value.split(<span class="regexp">/[\r\n]/</span>).sort().reverse().join(<span class="string">"\r\n"</span>);<span class="comment">//反序</span></span><br><span class="line">    K1.value=K1.value!=text?text:test;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>清除html代码中的脚本</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">clear_script</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    K1.value=K1.value.replace(<span class="regexp">/&lt;script.*?&gt;[\s\S]*?&lt;\/script&gt;|\s+on[a-zA-Z]&#123;3,16&#125;\s?=\s?"[\s\S]*?"|\s+on[a-zA-Z]&#123;3,16&#125;\s?=\s?'[\s\S]*?'|\s+on[a-zA-Z]&#123;3,16&#125;\s?=[^ &gt;]+/ig</span>,<span class="string">""</span>);</span><br><span class="line">&#125;</span><br><span class="line">动态执行JavaScript脚本</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">javascript</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">try</span>&#123;</span><br><span class="line">      <span class="built_in">eval</span>(K1.value);</span><br><span class="line">    &#125;<span class="keyword">catch</span>(e)&#123;</span><br><span class="line">      alert(e.message);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>金额大写转换函数</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transform</span>(<span class="params">tranvalue</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> i = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">var</span> dw2 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">""</span>, <span class="string">"万"</span>, <span class="string">"亿"</span>); <span class="comment">//大单位</span></span><br><span class="line">        <span class="keyword">var</span> dw1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">"拾"</span>, <span class="string">"佰"</span>, <span class="string">"仟"</span>); <span class="comment">//小单位</span></span><br><span class="line">        <span class="keyword">var</span> dw = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">"零"</span>, <span class="string">"壹"</span>, <span class="string">"贰"</span>, <span class="string">"叁"</span>, <span class="string">"肆"</span>, <span class="string">"伍"</span>, <span class="string">"陆"</span>, <span class="string">"柒"</span>, <span class="string">"捌"</span>, <span class="string">"玖"</span>); <span class="comment">//整数部分用</span></span><br><span class="line">        <span class="comment">//以下是小写转换成大写显示在合计大写的文本框中     </span></span><br><span class="line">        <span class="comment">//分离整数与小数</span></span><br><span class="line">        <span class="keyword">var</span> source = splits(tranvalue);</span><br><span class="line">        <span class="keyword">var</span> num = source[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">var</span> dig = source[<span class="number">1</span>];</span><br><span class="line">        <span class="comment">//转换整数部分</span></span><br><span class="line">        <span class="keyword">var</span> k1 = <span class="number">0</span>; <span class="comment">//计小单位</span></span><br><span class="line">        <span class="keyword">var</span> k2 = <span class="number">0</span>; <span class="comment">//计大单位</span></span><br><span class="line">        <span class="keyword">var</span> sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">var</span> str = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">var</span> len = source[<span class="number">0</span>].length; <span class="comment">//整数的长度</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= len; i++) &#123;</span><br><span class="line">              <span class="keyword">var</span> n = source[<span class="number">0</span>].charAt(len - i); <span class="comment">//取得某个位数上的数字</span></span><br><span class="line">              <span class="keyword">var</span> bn = <span class="number">0</span>;</span><br><span class="line">              <span class="keyword">if</span> (len - i - <span class="number">1</span> &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">                bn = source[<span class="number">0</span>].charAt(len - i - <span class="number">1</span>); <span class="comment">//取得某个位数前一位上的数字</span></span><br><span class="line">              &#125;</span><br><span class="line">              sum = sum + <span class="built_in">Number</span>(n);</span><br><span class="line">              <span class="keyword">if</span> (sum != <span class="number">0</span>) &#123;</span><br><span class="line">                str = dw[<span class="built_in">Number</span>(n)].concat(str); <span class="comment">//取得该数字对应的大写数字，并插入到str字符串的前面</span></span><br><span class="line">                <span class="keyword">if</span> (n == <span class="string">'0'</span>) sum = <span class="number">0</span>;</span><br><span class="line">              &#125;</span><br><span class="line">              <span class="keyword">if</span> (len - i - <span class="number">1</span> &gt;= <span class="number">0</span>) &#123; <span class="comment">//在数字范围内</span></span><br><span class="line">                <span class="keyword">if</span> (k1 != <span class="number">3</span>) &#123; <span class="comment">//加小单位</span></span><br><span class="line">                      <span class="keyword">if</span> (bn != <span class="number">0</span>) &#123;</span><br><span class="line">                        str = dw1[k1].concat(str);</span><br><span class="line">                      &#125;</span><br><span class="line">                      k1++;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123; <span class="comment">//不加小单位，加大单位</span></span><br><span class="line">                      k1 = <span class="number">0</span>;</span><br><span class="line">                      <span class="keyword">var</span> temp = str.charAt(<span class="number">0</span>);</span><br><span class="line">                      <span class="keyword">if</span> (temp == <span class="string">"万"</span> || temp == <span class="string">"亿"</span>) <span class="comment">//若大单位前没有数字则舍去大单位</span></span><br><span class="line">                      str = str.substr(<span class="number">1</span>, str.length - <span class="number">1</span>);</span><br><span class="line">                      str = dw2[k2].concat(str);</span><br><span class="line">                      sum = <span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;</span><br><span class="line">              <span class="keyword">if</span> (k1 == <span class="number">3</span>)&#123; <span class="comment">//小单位到千则大单位进一</span></span><br><span class="line">                k2++;</span><br><span class="line">              &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//转换小数部分</span></span><br><span class="line">        <span class="keyword">var</span> strdig = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">if</span> (dig != <span class="string">""</span>) &#123;</span><br><span class="line">              <span class="keyword">var</span> n = dig.charAt(<span class="number">0</span>);</span><br><span class="line">              <span class="keyword">if</span> (n != <span class="number">0</span>) &#123;</span><br><span class="line">                strdig += dw[<span class="built_in">Number</span>(n)] + <span class="string">"角"</span>; <span class="comment">//加数字</span></span><br><span class="line">              &#125;</span><br><span class="line">              <span class="keyword">var</span> n = dig.charAt(<span class="number">1</span>);</span><br><span class="line">              <span class="keyword">if</span> (n != <span class="number">0</span>) &#123;</span><br><span class="line">                strdig += dw[<span class="built_in">Number</span>(n)] + <span class="string">"分"</span>; <span class="comment">//加数字</span></span><br><span class="line">              &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        str += <span class="string">"元"</span> + strdig;</span><br><span class="line">    &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"0元"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> str;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//拆分整数与小数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">splits</span>(<span class="params">tranvalue</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> value = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">''</span>, <span class="string">''</span>);</span><br><span class="line">    temp = tranvalue.split(<span class="string">"."</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; temp.length; i++) &#123;</span><br><span class="line">        value = temp;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>获取窗体可见范围的宽与高</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getViewSize</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> de=<span class="built_in">document</span>.documentElement;</span><br><span class="line">    <span class="keyword">var</span> db=<span class="built_in">document</span>.body;</span><br><span class="line">    <span class="keyword">var</span> viewW=de.clientWidth==<span class="number">0</span> ?  db.clientWidth : de.clientWidth;</span><br><span class="line">    <span class="keyword">var</span> viewH=de.clientHeight==<span class="number">0</span> ?  db.clientHeight : de.clientHeight;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Array</span>(viewW ,viewH);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2016/10/20/review-jQuery/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2016/11/12/compare-js-php-func/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
